<template>
    <Sjdx v-if="show" :params="params" :active-sjdx="activeSjdx">
        <template #wjnr="data">
            <template v-if="data.$table">
                <el-image
                    v-if="data.row.wjlx === 'png' || data.row.wjlx === 'jpg' || data.row.wjlx === 'jpeg' || data.row.wjlx === 'gif'"
                    style="height:100%;width:100%;" :src="axios.getBaseURL()
                        + '/default?sjdx.dxdm=SYS_QX_QTQX&sys.cllx=download&yobj.id=' + data.row.id" :preview-src-list="[axios.getBaseURL() + '/default?sjdx.dxdm=SYS_QX_QTQX&sys.cllx=download&yobj.id=' +
        data.row.id]" fit="cover">
                    <template #error>
                        <el-icon style="font-size:22px;display:block;">
                            <PictureFilled />
                        </el-icon>
                    </template></el-image>
                <el-button v-else size="small" link type='primary' @click="download(data.row.id)">下载</el-button>

            </template>
            <div v-if="data.$form">
                <template v-if="data.data.id">
                    <el-image
                        v-if="data.data.wjlx === 'png' || data.data.wjlx === 'jpg' || data.data.wjlx === 'jpeg' || data.data.wjlx === 'gif'"
                        style="height:100%;width:100%;" :src="axios.getBaseURL()
                            + '/default?sjdx.dxdm=SYS_QX_QTQX&sys.cllx=download&yobj.id=' + data.data.id"
                        :preview-src-list="[axios.getBaseURL() + '/default?sjdx.dxdm=SYS_QX_QTQX&sys.cllx=download&yobj.id=' +
                            data.data.id]" fit="cover">
                        <template #error>
                            <el-icon style="font-size:22px;display:block;">
                                <PictureFilled />
                            </el-icon>
                        </template>
                    </el-image>
                    <el-button v-else size="small" link type='primary' @click="download(data.row.id)">下载</el-button>

                </template>
                <el-upload v-else ref="uploadRef" :auto-upload=false action="" :limit="1"
                    @change="(uploadFile: any, uploadFiles: any) => upload(uploadFile, uploadFiles, data)">
                    <el-button size="small" type="primary">上传</el-button>
                </el-upload>
            </div>
        </template>
    </Sjdx>
</template>
<script setup lang="ts">
import {ref, watch} from "vue";
import Sjdx from "@/components/Sjdx";
import axios from "@/axios";
import { PictureFilled } from "@element-plus/icons-vue";
import {useRoute} from "vue-router";
const show = ref(false);
const params = {
  'sys.authCode': 'KFZFW_PTGL_WJGL'
}
const activeSjdx = ref(undefined);
const download = (id: string) => {
    let params = {
        sjdx: {
            dxdm: 'SYS_QX_QTQX'
        },
        sys: {
            cllx: 'download',
        },
        yobj: {
            id: id
        }
    }
    axios.download(params)
}
const upload = (uploadFile: any, uploadFiles: any, data: any) => {
    console.log(uploadFile)
    getBase64(uploadFile.raw).then((res: any) => {
        console.log(data.data)
        data.data.wjm = uploadFile.name
        data.data.wjlx = uploadFile.name.split('.').pop()
        data.data.wjnr = res.substring(res.indexOf(",") + 1)
        console.log("转换base64成功")
    })
}
const getBase64 = (file: any) => {
    return new Promise((resolve, reject) => {
        let reader = new FileReader()	//定义方法读取文件
        reader.readAsDataURL(file)	//开始读文件  本身是图片的二进制数据 进行base64加密形成字符串
        reader.onload = () => resolve(reader.result)//成功返回对应的值 reader.result可以直接放在img标签中使用
        reader.onerror = () => reject(reader.error)//失败返回失败信息
    })
}
const route = useRoute() as any;
watch(
    () => route.meta,
    async newQuery => {
      if(route.meta.authCode===params["sys.authCode"]){
        activeSjdx.value = NaN+route.meta.authCode
      }else {
        activeSjdx.value = undefined
      }
    }
)
setTimeout(() => {
    show.value = true
}, 0)
</script>